package net.ib.mtalk.network.task;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Session.SetBuddyInfoReq;
import net.ib.mntalk.protocol.Session.SetBuddyInfoRes;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.database.helper.BuddyDatabaseHelper;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.util.MTalkLogWriter;
import net.ib.mtalk.util.NotifyingAsyncQueryHandler;
import android.content.Context;
import android.os.Handler;

public class SetBuddyinfoTask extends AsyncNetTask {
	
	private NotifyingAsyncQueryHandler mDbHandler;
	private String firstName, lastName, middlename, namePrefix, nameSuffix, phoneticFamilyName, phoneticGivenNmae, phoneticMiddleName, identifier, bio, status, country, area;
	private int type, index, gender, calendarType, era, year, month, day, presence, mood;
	private double accuracy, altitude, longitude, latitude;
	private SetBuddyInfoReq mSetBuddyInfoReq;
	
	public SetBuddyinfoTask(Context _context, Handler _uiHandler, SetBuddyInfoReq setBuddyInfoReq) {
		super(_context, _uiHandler);
		mContext = _context;
		mUiHandler = _uiHandler;
		
		this.mSetBuddyInfoReq = setBuddyInfoReq;
	}

	@Override
	protected TcpEnvelope onPreExecute() {
		TcpEnvelope rev = new TcpEnvelope.Builder()
			.setType(TcpEnvelope.REQUEST)
			.setMessage(mSetBuddyInfoReq)
			.setMethodId(Session.Contact.SetBuddyInfo)
			.build();
		return rev;
	}

	@Override
	protected void onPostExecute(int resultCode, Object sentObject,
			Object receivedObject) {
		MTalkLogWriter.d("SetBuddyInfoTask.onPostExecute() resultCode : " + resultCode);
		if (receivedObject == null) {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNKNOWN_ERROR));
			return;
		}
		
		TcpEnvelope resultEnvelope = (TcpEnvelope) receivedObject;
		SetBuddyInfoRes setBuddyInfoRes = (SetBuddyInfoRes) resultEnvelope.getMessage();
		
		MTalkLogWriter.d("SetBuddyInfoTask ResponseCode : " + setBuddyInfoRes.getResponseCode());
		
		if(setBuddyInfoRes.getResponseCode() == Common.SUCCESS){
			// TODO : 디비작업
			BuddyDatabaseHelper.updateBuddyStatus(mContext, mSetBuddyInfoReq);
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_SET_BUDDY_INFO_SUCCESS));
//			BuddyDatabaseHelper
		} else {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_SET_BUDDY_INFO_FAIL));
		}
	}

}
